<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="netty高性能网络框架-Netty的组建和设计 在Netty框架中有三大关键组建 Channel EventLoop channelFuture ChannelHandler channelPipeline bootstrap  如果和网络中的相关概念相互对应将会是如下的结果：  Channel —— Socket  EventLoop —— 控制流、多线程处理、并发  ChannelFutur">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="netty高性能网络框架-Netty的组建和设计 在Netty框架中有三大关键组建 Channel EventLoop channelFuture ChannelHandler channelPipeline bootstrap  如果和网络中的相关概念相互对应将会是如下的结果：  Channel —— Socket  EventLoop —— 控制流、多线程处理、并发  ChannelFutur">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/blogimg/netty/1.png">
<meta property="og:image" content="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/blogimg/netty/2.png">
<meta property="og:image" content="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/blogimg/netty/3.png">
<meta property="og:updated_time" content="2018-10-09T07:22:37.337Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="netty高性能网络框架-Netty的组建和设计 在Netty框架中有三大关键组建 Channel EventLoop channelFuture ChannelHandler channelPipeline bootstrap  如果和网络中的相关概念相互对应将会是如下的结果：  Channel —— Socket  EventLoop —— 控制流、多线程处理、并发  ChannelFutur">
<meta name="twitter:image" content="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/blogimg/netty/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/netty高性能网络框架-Netty的组建和设计/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="netty高性能网络框架-Netty的组建和设计"><a href="#netty高性能网络框架-Netty的组建和设计" class="headerlink" title="netty高性能网络框架-Netty的组建和设计"></a>netty高性能网络框架-Netty的组建和设计</h2><blockquote>
<p>在Netty框架中有三大关键组建 Channel EventLoop channelFuture ChannelHandler channelPipeline bootstrap</p>
</blockquote>
<p>如果和网络中的相关概念相互对应将会是如下的结果：</p>
<ul>
<li><p>Channel —— Socket</p>
</li>
<li><p>EventLoop —— 控制流、多线程处理、并发</p>
</li>
<li><p>ChannelFuture —— 异步通知</p>
</li>
</ul>
<h3 id="一-Chanel接口"><a href="#一-Chanel接口" class="headerlink" title="一.Chanel接口"></a>一.Chanel接口</h3><p>这个接口实现基本的io操作(bind()、connect()、read()、write())，映射传统javaSocket的编程过程，大大降低相关的直接使用socket的复杂程度。相关的实现类</p>
<ul>
<li>NioSocketChannel</li>
<li>NioSocketServerChannel</li>
</ul>
<h3 id="二-EventLoop接口"><a href="#二-EventLoop接口" class="headerlink" title="二.EventLoop接口"></a>二.EventLoop接口</h3><p>这个接口相当于一个人容器，控制链接生命周期所发生的相关所有的事件。</p>
<p>Channel、EventLoop、Thread、EventLoopGroup之间的关系如下图： </p>
<p><img src="blogimg/netty/1.png" alt=""></p>
<h3 id="三-ChannelFuure接口"><a href="#三-ChannelFuure接口" class="headerlink" title="三.ChannelFuure接口"></a>三.ChannelFuure接口</h3><p>在netty中处理I/O操作异步返回值的方法就是ChannelFutrue接口，这个接口的addListener方法可以注册一个ChannelFutureListener方法，方便在某个操作完成的时候得到通知</p>
<h3 id="四-ChannelHandler接口"><a href="#四-ChannelHandler接口" class="headerlink" title="四.ChannelHandler接口"></a>四.ChannelHandler接口</h3><p>管理数据流和执行应用程序逻辑的重要组件，ChannelHandler充当了所有处理入站和出站相关应用的逻辑处理的容器</p>
<p>有两个非常常用的子接口</p>
<p>ChannelInBoundHandler和ChannelOutboundHandler分别代表出站和入站的相关处理逻辑</p>
<h3 id="五-ChannelPipeline接口"><a href="#五-ChannelPipeline接口" class="headerlink" title="五.ChannelPipeline接口"></a>五.ChannelPipeline接口</h3><p>chanelPpeline接口为ChannelHandler链提供了容器，并定义了用在该链上传播入站和出站时间流的api ，当channl被创建的时候将自动的分配到他专属的ChannelPipeline中被相关的channlhandle处理</p>
<p>ChnanelPipeline中流动的是事件（事件中可能附加数据）。Netty定义了两种事件类型：入站（inbound）事件和出站（outbound）事件。ChannelPipeline使用拦截过滤器模式使用户可以掌控ChannelHandler处理事件的流程。注意：事件在ChannelPipeline中不自动流动而需要调用ChannelHandlerContext中诸如fileXXX()或者read()类似的方法将事件从一个ChannelHandler传播到下一个ChannelHandler</p>
<p><img src="blogimg/netty/2.png" alt=""></p>
<blockquote>
<p>特殊东西ChannelHandlerContext</p>
</blockquote>
<p>Context指上下文关系，ChannelHandler的Context指的是ChannleHandler之间的关系以及ChannelHandler与ChannelPipeline之间的关系。ChannelPipeline中的事件传播主要依赖于ChannelHandlerContext实现，由于ChannelHandlerContext中有ChannelHandler之间的关系，所以能得到ChannelHandler的后继节点，从而将事件传播到下一个ChannelHandler</p>
<h3 id="六-引导bootstrap"><a href="#六-引导bootstrap" class="headerlink" title="六.引导bootstrap"></a>六.引导bootstrap</h3><blockquote>
<p>引导字如其意，就是进行相关链接处理的工具方法</p>
</blockquote>
<table>
<thead>
<tr>
<th>类别</th>
<th>BootStrap</th>
<th>ServerBootStrap</th>
</tr>
</thead>
<tbody>
<tr>
<td>网络中编程的作用</td>
<td>链接远程的主机和端口</td>
<td>绑定到一个本地端口</td>
</tr>
<tr>
<td>EventLoopGroup的数量</td>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
<p><strong>注意</strong>:这里服务端的LoopGroup需要两个，,默认有两个eventloop分别是bossGroup和 workGroup，其中boss用来监控tcp链接,worker用来处理io事件。</p>
<p><img src="blogimg/netty/3.png" alt=""></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/plantUML-流程图画法/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/mysql数据库开启远程登入权限/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#netty高性能网络框架-Netty的组建和设计"><span class="nav-number">1.</span> <span class="nav-text">netty高性能网络框架-Netty的组建和设计</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一-Chanel接口"><span class="nav-number">1.1.</span> <span class="nav-text">一.Chanel接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二-EventLoop接口"><span class="nav-number">1.2.</span> <span class="nav-text">二.EventLoop接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三-ChannelFuure接口"><span class="nav-number">1.3.</span> <span class="nav-text">三.ChannelFuure接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四-ChannelHandler接口"><span class="nav-number">1.4.</span> <span class="nav-text">四.ChannelHandler接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#五-ChannelPipeline接口"><span class="nav-number">1.5.</span> <span class="nav-text">五.ChannelPipeline接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#六-引导bootstrap"><span class="nav-number">1.6.</span> <span class="nav-text">六.引导bootstrap</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
